package com.elinkway.infinitemovies.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bigdata.RealDataApi;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.ui.activity.RankDetailActivity;
import com.elinkway.infinitemovies.ui.activity.TopicDetailActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityFroWebView;

/**
 * Created by richard on 2017/8/25.
 */

public class PushUtil {

    public static final String TAB_INDEX_HOME = "1";
    public static final String TAB_INDEX_ONEPOINT = "6";
    public static final String TAB_INDEX_TOPIC = "3";
    public static final String EXTRA_PUSH_NOTIFICATION = "push_notification";

    public static class PushParam {
        public static final String NAME = "name";
        public static final String THEME_ID = "themeId";
        public static final String VIDEO_ID = "videoId";
        public static final String VIDEO_TYPE = "videoType";
        public static final String EPISODE_NUMBER = "episodeNumber";
        public static final String URL = "url";
        public static final String TITLE = "title";
        public static final String SUMMARY = "summary";
        public static final String PIC = "pic";
        public static final String FROM = "from";
    }

    public static void doPush(Intent intent) {
        boolean isFinishInPlaySelf = false;
        String actionType = intent.getStringExtra(Utils.ACTION_TYPE);
        if (TextUtils.isEmpty(intent.getStringExtra(Utils.ACTION_TYPE))) {
            return;
        }

        String runningActivityName = MoviesApplication.getInstance().getRunningActivityName();
        Activity activity = MoviesApplication.getInstance().getActivity();
        Activity topActivity = MoviesApplication.getInstance().getTopActivity();
        Activity videoDetailActivity = MoviesApplication.getInstance().getVideoDetailActivity();

        if (MoviesApplication.PLAY_VIDEO_THREAD.equals(runningActivityName) && topActivity != null) {
            topActivity.finish();
            isFinishInPlaySelf = true;
        }

        if (Utils.TOPIC_DETAIL_ACTIVITY.equals(intent.getStringExtra(Utils.ACTION_TYPE))) {
            if (MoviesApplication.TOPIC_DETAIL_THREAD.equals(runningActivityName) && topActivity != null) {
                topActivity.finish();
            }
            String themeId = intent.getStringExtra(PushParam.THEME_ID);
            String name = intent.getStringExtra(PushParam.NAME);
            TopicDetailActivity.launch(activity, themeId, name, true);
        } else if (Utils.VIDEO_DETAIL_ACTIVITY.equals(actionType)) {
            LogUtils.e("Push VideoDetail " + intent.getStringExtra("videoId") + " " + intent.getStringExtra("videoType")
                    + " " + runningActivityName + " " + activity);
            if ((MoviesApplication.VIDEO_DETAIL_THREAD.equals(runningActivityName) && videoDetailActivity != null)
                    || (isFinishInPlaySelf && videoDetailActivity != null)) {
                videoDetailActivity.finish();
            }
            String videoId = intent.getStringExtra(PushParam.VIDEO_ID);
            RealDataApi.RealDataClickReport("", "", "", videoId, "");
            String videoType = intent.getStringExtra(PushParam.VIDEO_TYPE);
            String name = intent.getStringExtra(PushParam.NAME);
            String themeId = intent.getStringExtra(PushParam.THEME_ID);
            String episodeNumber = intent.getStringExtra(PushParam.EPISODE_NUMBER);
            VideoDetailActivity.launch(activity, videoId, videoType, name, "", themeId, "", episodeNumber, "", "", true);
        } else if (Utils.RANK_DETAIL_ACTIVITY.equals(actionType)) {
            if (MoviesApplication.RANK_DETAIL_THREAD.equals(runningActivityName) && topActivity != null) {
                topActivity.finish();
            }
            String videoType = intent.getStringExtra(PushParam.VIDEO_TYPE);
            RankDetailActivity.launch(activity, videoType, true);
        } else if (Utils.PLAY_ACTIVITY.equals(actionType)) {
            LogUtils.e("Push Enter PlayActivitySelfOwn");
        } else if (Utils.WEB_ACTIVITY.equals(actionType)) {
            LogUtils.e("Push jump Web");
            String url = intent.getStringExtra(PushParam.URL);
            String title = intent.getStringExtra(PushParam.TITLE);
            Utils.jumpWebViewActivity(activity, url, title, true);
        } else if (Utils.MAIN_ACTIVITY.equals(actionType)) {
            LogUtils.e("Push Enter shouldOverrideUrlLoading MAIN");
            startMainActivityFromPush(TAB_INDEX_HOME);
        } else if (Utils.TOPIC_ACTIVITY.equals(actionType)) {
            startMainActivityFromPush(TAB_INDEX_TOPIC);
        } else if (Utils.SHORT_VIDEO.equals(actionType)) {
            startWebActivityFromPush(intent);
        }
    }

    private static void startMainActivityFromPush(String value) {
        Context context = MoviesApplication.getInstance().getActivity();
        if (context == null) {
            context = MoviesApplication.getInstance();
        }
        Intent intent = new Intent(context, NewMainActivity.class);
        intent.putExtra(EXTRA_PUSH_NOTIFICATION, value);
        intent.putExtra(MoviesApplication.START_FROM_NOTIFICATION, true);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private static void startWebActivityFromPush(Intent pushIntent) {
        Context context = MoviesApplication.getInstance().getActivity();
        if (context == null) {
            context = MoviesApplication.getInstance();
        }
        Intent intent = new Intent(context, PlayActivityFroWebView.class);
        String imageUrl = pushIntent.getStringExtra(PushParam.PIC);
        String title = pushIntent.getStringExtra(PushParam.TITLE);
        String summary = pushIntent.getStringExtra(PushParam.SUMMARY);
        String url = pushIntent.getStringExtra(PushParam.URL);
        intent.putExtra(PushParam.TITLE, title);
        intent.putExtra(PushParam.SUMMARY, summary);
        intent.putExtra(PushParam.PIC, imageUrl);
        intent.putExtra(PushParam.URL, url);
        intent.putExtra(PushParam.FROM, MoviesConstant.ONEPOINT_TAG);
        intent.putExtra(MoviesApplication.START_FROM_NOTIFICATION, true);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
}
